import { Body, Controller, Delete, Get, Param, ParseIntPipe, Post, Put } from '@nestjs/common';
import { DictionaryItemService } from './dictionary-item.service';
import { DtoPipe } from 'apps/admin/src/pipe/dto/dto.pipe';
import CreateDto from './dto/create.dto';
import { ResponseUtilService } from '@app/response-util';
import UpdateDto from './dto/update.dto';

@Controller('dictionary/item')
export class DictionaryItemController {
    constructor(
        private readonly service:DictionaryItemService
    ){}

    @Post('add')
    async add(@Body(new DtoPipe()) dto:CreateDto) {
        try {
            const r = await this.service.add(dto)
            return r ? ResponseUtilService.successRes() : ResponseUtilService.failRes()
        } catch (error) {
            return ResponseUtilService.failRes(error.message)
        }
    }

    @Put('update')
    async update(@Body(new DtoPipe()) dto:UpdateDto) {
        try {
            const r = await this.service.update(dto)
            return r ? ResponseUtilService.successRes() : ResponseUtilService.failRes()
        } catch (error) {
            return ResponseUtilService.failRes(error.message)
        }
    }

    @Delete('delete/:id')
    async del(@Param('id', ParseIntPipe) id:number) {
        if(id < 1) return null
        try {
            const r = await this.service.del(id)
            return r ? ResponseUtilService.successRes() : ResponseUtilService.failRes()
        } catch (error) {
            return ResponseUtilService.failRes(error.message)
        } 
    }


    @Get(':category')
    async items(@Param('category') category:string) {
        const items = await this.service.getItemByCategory(category)
        return ResponseUtilService.successRes(items)
    }
}
